package com.kevin.ymqquan.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.SQLException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kevin.ymqquan.R;
import com.kevin.ymqquan.activity.InfoActivity;
import com.kevin.ymqquan.activity.LoginActivity;
import com.kevin.ymqquan.activity.MaCourseActivity;
import com.kevin.ymqquan.activity.ManageClubActivity;
import com.kevin.ymqquan.activity.MyClubActivity;
import com.kevin.ymqquan.activity.MyCourseActivity;
import com.kevin.ymqquan.application.App;
import com.kevin.ymqquan.custom.CustomToast;

/**
 * home3
 * 
 * @author andye
 * 
 */
public class Home3Fra extends Fragment implements OnClickListener {

	private RelativeLayout rMyInfo;

	private RelativeLayout rMyClub;
	private RelativeLayout rmanageClub;

	private RelativeLayout rmanageCourse;
	private RelativeLayout rmyCourse;

	private RelativeLayout rLogout;
	private static final String LOGIN = "login_pref";
	private App application;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_my, null);

		init(v);

		return v;
	}

	private void init(View v) {
		application = (App) getActivity().getApplication();
		rMyInfo = (RelativeLayout) v.findViewById(R.id.rl_my_myinfo);

		rMyClub = (RelativeLayout) v.findViewById(R.id.rl_my_myclub);
		rmanageClub = (RelativeLayout) v.findViewById(R.id.rl_my_manageclub);

		rmyCourse = (RelativeLayout) v.findViewById(R.id.rl_my_mycourse);
		rmanageCourse = (RelativeLayout) v
				.findViewById(R.id.rl_my_managecourse);

		rLogout = (RelativeLayout) v.findViewById(R.id.rl_my_logout);

		rMyClub.setOnClickListener(this);
		rMyInfo.setOnClickListener(this);
		rmyCourse.setOnClickListener(this);
		rmanageClub.setOnClickListener(this);
		rmanageCourse.setOnClickListener(this);
		rLogout.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_my_myinfo:
			
			Intent mIntents = new Intent(getActivity(), InfoActivity.class);
			startActivity(mIntents);
			break;
		case R.id.rl_my_myclub:
			// CustomToast.CustomActivityToast(getActivity(), "我的俱乐部", -80);
			Intent i = new Intent(getActivity(), MyClubActivity.class);
			startActivity(i);
			break;

		case R.id.rl_my_manageclub:
			Intent mIntent = new Intent(getActivity(), ManageClubActivity.class);
			startActivity(mIntent);
			break;

		case R.id.rl_my_mycourse:
			Intent mycourse = new Intent(getActivity(), MyCourseActivity.class);
			startActivity(mycourse);
//			CustomToast.CustomActivityToast(getActivity(), "我的课程", -80);
			break;
			
		case R.id.rl_my_managecourse:
			Intent macourse = new Intent(getActivity(), MaCourseActivity.class);
			startActivity(macourse);
			break;
			
		case R.id.rl_my_logout:

			logout();
			break;

		default:
			CustomToast.CustomActivityToast(getActivity(), "异常", -80);
			break;
		}
	}

	private void logout() {
		// TODO Auto-generated method stub
		LayoutInflater inflaterDl = LayoutInflater.from(getActivity());
		LinearLayout inflate = (LinearLayout) inflaterDl.inflate(
				R.layout.activity_logout_dialog, null);
		final Dialog dialog = new AlertDialog.Builder(getActivity()).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(inflate);
		TextView tv_setting_update_dialog_title = (TextView) inflate
				.findViewById(R.id.tv_logout_dialog_title);
		tv_setting_update_dialog_title.setText("确定要注销登录吗？");
		TextView tv_dialog_left_btn = (TextView) inflate
				.findViewById(R.id.tv_dialog_left_btn);
		tv_dialog_left_btn.setText("确认");
		TextView tv_dialog_right_btn = (TextView) inflate
				.findViewById(R.id.tv_dialog_right_btn);
		tv_dialog_right_btn.setText("取消");

		RelativeLayout rl_setting_update_dialog_cancel = (RelativeLayout) inflate
				.findViewById(R.id.ll_logout_dialog_cancel);
		RelativeLayout rl_setting_update_dialog_ensure = (RelativeLayout) inflate
				.findViewById(R.id.ll_logout_dialog_ok);
		rl_setting_update_dialog_ensure
				.setOnClickListener(new View.OnClickListener() {

					@SuppressWarnings("null")
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						// 显示下载对话框
						try {
							clearAndExit();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});

		rl_setting_update_dialog_cancel
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
	}

	protected void clearAndExit() {

		// 清空数据
		SharedPreferences preferences = getActivity().getSharedPreferences(
				LOGIN, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putString("userId", "0");
		editor.putString("userName", "");
		editor.putString("passWord", "");
		editor.commit();

		application.userId = 0;
		// 关闭所有并打开LoginActivity
		application.exit();
		Intent intent = new Intent(getActivity(), LoginActivity.class);
		startActivity(intent);
		this.getActivity().finish();
	}

}
